package com.example.administrator.x2picturetransliteration.Home.Presenter.Fragment;

import android.content.Context;

import com.alibaba.fastjson.JSON;
import com.example.administrator.x2picturetransliteration.Base.BasePresenter;
import com.example.administrator.x2picturetransliteration.Base.HttpGo;
import com.example.administrator.x2picturetransliteration.Bean.HttpDataBean;
import com.example.administrator.x2picturetransliteration.Bean.UserWorkListBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import rx.subscriptions.CompositeSubscription;

/**
 * 文档
 * Created by Administrator on 2019/9/6.
 */

public class FilePresenter extends BasePresenter{

    public FilePresenter(Context context, CompositeSubscription mCompositeSubscription) {
        super(context, mCompositeSubscription);
    }

    /** 文档列表 */
    private Integer listPageNum = 1;
    public void list(boolean Refresh,Map<String,String> httpdata, String msg,final Callback mCallback){

        if (Refresh)listPageNum = 1;

        httpdata.put("isTemp","0");
        httpdata.put("page",listPageNum + "");
        httpdata.put("limit","10");

        HttpGo.HttpGoCallback mHttpGoCallback = new HttpGo.HttpGoCallback() {
            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(HttpDataBean httpDataBean) {

                if (httpDataBean.getCode()==0){

                    if (httpDataBean.getData()!=null){

                        List<UserWorkListBean> listdata = JSON.parseArray(httpDataBean.getData() + "", UserWorkListBean.class);

                        if (listdata.size()>0)listPageNum = listPageNum + 1;

                        mCallback.getData(listdata);
                    }
                }
            }
        };

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user_works/list", httpdata, null,mHttpGoCallback)
        );

    }

    /** 文档列表 */
    private Integer listdraftPageNum = 1;
    public void listdraft(boolean Refresh,Map<String,String> httpdata, final Callback mCallback){

        if (Refresh)listdraftPageNum = 1;

        httpdata.put("isTemp","1");
        httpdata.put("page",listdraftPageNum + "");
        httpdata.put("limit","10");

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user_works/list", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            if (httpDataBean.getData()!=null){

                                List<UserWorkListBean> listdata = JSON.parseArray(httpDataBean.getData() + "", UserWorkListBean.class);

                                if (listdata.size()>0)listdraftPageNum = listdraftPageNum + 1;

                                mCallback.getData(listdata);
                            }
                        }
                    }
                })
        );
    }

    /** 删除 */
    public void del(String ids, final Callback mCallback){

        Map<String,String> httpdata = new HashMap<>();
        httpdata.put("ids",ids);

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user_works/del", httpdata,"删除成功", new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0){

                            mCallback.getData(null);
                        }
                    }
                })
        );

    }

    /** 保存文档(重命名) */
    public void addwork(Map<String,String> httpdata, final Callback mCallback){

        mCompositeSubscription.add(

                mHttpGo.postSubscription("user_works/add", httpdata, new HttpGo.HttpGoCallback() {
                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpDataBean httpDataBean) {

                        if (httpDataBean.getCode()==0)mCallback.getData(httpDataBean.getData());
                    }
                })
        );
    }



}
